package com.ibm.law.ui.common;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.widgets.Table;

import com.ibm.law.domain.Model;

public class CommonEditingSupport extends EditingSupport {
	private TableViewerColumn viewerColumn;
	private Model model;
	private String column;
	private TableViewer viewer;

	public CommonEditingSupport(TableViewer viewer,
			TableViewerColumn viewerColumn, Model model) {
		super(viewer);
		this.viewerColumn = viewerColumn;
		this.viewer = viewer;
		column = (String) viewerColumn.getColumn().getData("columnName");
		this.model = model;
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		String editorType = model.getEditors().get(column);
		if ("TEXT".equals(editorType)) {
			return new TextCellEditor((Table) this.viewer.getControl());
		} else if ("CHECKBOX".equals(editorType)) {
			return new CheckboxCellEditor((Table) this.viewer.getControl());
		} else if ("COMBO".equals(editorType)) {
			return new ComboBoxCellEditor((Table) this.viewer.getControl(),
					model.getCandidateList(column));
		}
		return null;
	}

	@Override
	protected boolean canEdit(Object element) {
		boolean editable = model.getEditors().get(column) != null;
		return editable;
	}

	@Override
	protected Object getValue(Object element) {
		try {
			Object value = PropertyUtils.getProperty(element, column);

			if (model.getEditors().get(column).equals("CHECKBOX")) {
				return value == null ? false : value;
			}
			if (model.getEditors().get(column).equals("COMBO")) {
				String[] lists = model.getCandidateList(column);
				for (int i = 0; i < lists.length; i++) {
					if (lists[i].equals(value)) {
						return i;
					}
				}
				return 0;
			}
			if (model.getEditors().get(column).equals("TEXT")) {
				return value + "";
			}
			return value;
		} catch (IllegalAccessException | InvocationTargetException
				| NoSuchMethodException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void setValue(Object element, Object value) {
		System.out.println(getValue(element));
		System.out.println(value);
		try {
			if (model.getEditors().get(column).equals("COMBO")) {
				String[] lists = model.getCandidateList(column);
				Integer index = (Integer) value;
				BeanUtils.setProperty(element, column, lists[index]);
			} else {
				BeanUtils.setProperty(element, column, value.toString());
			}
			viewer.refresh(element);
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
	}

}
